package controller;

import entity.h_complain;
import entity.h_room;
import entity.h_transaction;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import service.HotelService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/hotel")
public class HotelController {
    @Resource
    private HotelService hotelService;


    @RequestMapping("/hotelList")
    public String hotelList(){ return "index"; }
    @RequestMapping("/left1")
    public String left1(){
        return "left1";
    }
    @RequestMapping("/tab")
    public String tab(){
        return "tab";
    }
    @RequestMapping("/footer")
    public String footer(){
        return "footer";
    }
    @RequestMapping("/top1")
    public String top1(){
        return "top1";
    }
    @RequestMapping("/basic")
    public String basic(@RequestParam(required = false) String roomname,
                        @RequestParam(required = false) String roomstar,
                        HttpSession session,
                        Model model) {
        model.addAttribute("roomname",roomname);
        model.addAttribute("roomstar",roomstar);
        if("0".equals(roomstar)){
            roomstar="";
        }
        System.out.println(roomname+","+roomstar);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("roomname",roomname);
        map.put("roomstar",roomstar);
        List<h_room> list=hotelService.getH_roomAll(map);
        System.out.println(list);
        session.setAttribute("roomList",list);
        return "hotel/basic";
    }
    @RequestMapping("/basics")
    public String chaxun(){
        return "redirect:/hotel/basic";
    }
    @RequestMapping("/rooms")
    public String rooms(@RequestParam(required = false) String roomname,
                        @RequestParam(required = false) String roomstar,
                        @RequestParam(required = false,value = "kong") String kong,
                        HttpSession session,
                        Model model) {
        if("0".equals(roomstar)){
            roomstar="";
        }
        model.addAttribute("roomname",roomname);
        model.addAttribute("roomstar",roomstar);
        model.addAttribute("kong",kong);
        System.out.println(roomname+","+roomstar+","+kong);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("kong",kong);
        map.put("roomname",roomname);
        map.put("roomstar",roomstar);
        List<h_room> list=hotelService.getRoomsAll(map);
        System.out.println(list);
        session.setAttribute("roomsList",list);
        return "hotel/rooms";
    }
    @RequestMapping("/tradingSearch")
    public String tradingSearch(@RequestParam(required = false) String roomname,
                                @RequestParam(required = false) String roomstar,
                                @RequestParam(required = false) String pastTime,
                                @RequestParam(required = false) String futureTime,
                                HttpSession session,
                                Model model){
        if("0".equals(roomstar)){
            roomstar="";
        }
        model.addAttribute("roomname",roomname);
        model.addAttribute("roomstar",roomstar);
        model.addAttribute("pastTime",pastTime);
        model.addAttribute("futureTime",futureTime);
        System.out.println(roomname+","+roomstar+","+pastTime+","+futureTime);
        Map<String,Object> map = new HashMap<String,Object>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        if(pastTime!=null
                &&pastTime!=""){
            try {
                Date pastTime1=sdf.parse(pastTime);
                map.put("pastTime",pastTime1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            map.put("pastTime","");
        }
        if(futureTime!=null
                &&futureTime!=""){
            try {
                Date futureTime1=sdf.parse(futureTime);
                map.put("futureTime",futureTime1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            map.put("futureTime","");
        }
        map.put("roomname",roomname);
        map.put("roomstar",roomstar);
        System.out.println(map);
        List<h_transaction> list=hotelService.getTransactionAll(map);
        System.out.println(list);
        session.setAttribute("transactionList",list);
        return "hotel/tradingSearch";
    }
    @RequestMapping("/complaintsSearch")
    public String complaintsSearch(@RequestParam(required = false) String roomname,
                                @RequestParam(required = false) String roomstar,
                                @RequestParam(required = false) String pastTime,
                                @RequestParam(required = false) String futureTime,
                                HttpSession session,
                                Model model){
        if("0".equals(roomstar)){
            roomstar="";
        }
        model.addAttribute("roomname",roomname);
        model.addAttribute("roomstar",roomstar);
        model.addAttribute("pastTime",pastTime);
        model.addAttribute("futureTime",futureTime);
        System.out.println(roomname+","+roomstar+","+pastTime+","+futureTime);
        Map<String,Object> map = new HashMap<String,Object>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        if(pastTime!=null
                &&pastTime!=""){
            try {
                Date pastTime1=sdf.parse(pastTime);
                map.put("pastTime",pastTime1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            map.put("pastTime","");
        }
        if(futureTime!=null
                &&futureTime!=""){
            try {
                Date futureTime1=sdf.parse(futureTime);
                map.put("futureTime",futureTime1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            map.put("futureTime","");
        }
        map.put("roomname",roomname);
        map.put("roomstar",roomstar);
        System.out.println(map);
        List<h_complain> list=hotelService.getComplainAll(map);
        session.setAttribute("complainList",list);
        return "hotel/complaintsSearch";
    }
    @RequestMapping("/addHotel")
    public String addHotel(@ModelAttribute h_room room){
        return "hotel/addHotel";
    }
    @RequestMapping("/modifyHotel")
    public String modifyHotel(String roomid, Model model,@ModelAttribute h_room room){
        System.out.println("id------>>>>>>"+roomid);
        h_room room1=hotelService.getRoomById(roomid);
        model.addAttribute("room",room1);
        return "hotel/modifyHotel";
    }
    @RequestMapping("/modifyHotelList")
    @ResponseBody
    public Object modifyHotelList(h_room room,
                                  @RequestParam(name = "buildTime",required = false) String buildTime){
        if(buildTime!=""&&buildTime!=null){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date=sdf.parse(buildTime);
                room.setHo_room_buildTime(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        System.out.println("room---->>>>>"+room);
        if (hotelService.updateRoom(room)){
            return  "true";
        }else {
            return "false";
        }
    }
    @RequestMapping("/viewHotel")
    public String viewHotel(String roomid, Model model){
        System.out.println("id------>>>>>>"+roomid);
        h_room room1=hotelService.getRoomById(roomid);
        model.addAttribute("room",room1);
        return "hotel/viewHotel";
    }
    @RequestMapping("/deleteHotel")
    @ResponseBody
    public Object deleteHotel(String roomid){
        System.out.println("id------>>>>>>"+roomid);
        if (hotelService.deleteRoomById(roomid)){
            return "true";
        }else{
            return "false";
        }
    }
    @RequestMapping(value = "/insertHotel",method = RequestMethod.POST)
    @ResponseBody
    public Object insertHotel(h_room room,
                              @RequestParam(name="ccc") MultipartFile path,
                              HttpServletRequest request){
        System.out.println("添加信息");
        String filePath = null;
        //用来区分错误提示信息
        String errorInfo=null;
        // 先获取当前请求的全路径
        String p = request.getSession().getServletContext()
                .getRealPath("static" + File.separator + "uploadFile");
        System.out.println(p);
        // 解决用户上传的文件名和现在服务器上保存的
        // 文件名冲突的问题
        // 先获取旧的文件名
        String oldFileName = path.getOriginalFilename();
        System.out.println(oldFileName);
        // 获取旧文件名的后缀
        String prefix = FilenameUtils.getExtension(oldFileName);
        System.out.println(prefix);// png
        // 判断上传的文件大小是否超出规定
        if (path.getSize() > 50000000) {// 是否超过500k
            System.out.println("图片过大");
            return "error";
        }
        // 判断后缀是否符合要求
        if (prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png")
                || prefix.equalsIgnoreCase("gif")) {
            // 统一改名+后缀
            String newFileName =
                    // 当前系统的毫秒值+随机数+后缀
                    System.currentTimeMillis() + RandomUtils.nextInt(100000) + "." + prefix;
            // 使用File对象读取p路径下面的指定文件
            File file = new File(p, newFileName);
            if (!file.exists()) {
                // 创建多级目录
                file.mkdirs();
            }
            // 目录创建成功之后,将上传的文件保存起来
            try {
                path.transferTo(file);
            } catch (IllegalStateException | IOException e) {
                request.setAttribute(errorInfo, "上传失败");
                return "error";
                // e.printStackTrace();
            }
            // 给文件字段赋值
            filePath = p + File.separator + newFileName;
            room.setHo_room_picture(filePath);
        }else{
            System.out.println("filePath："+filePath);
            return "error";
        }

        if (hotelService.addRoom(room)){
            return "true";
        }else {
            return "false";
        }

    }

}
